package com.epam.task.web.service.command;

import com.epam.task.web.db.DataBaseWriter;
import com.epam.task.web.entity.Order;
import com.epam.task.web.entity.OrderProduct;
import com.epam.task.web.entity.Status;
import com.epam.task.web.entity.User;
import com.epam.task.web.service.AbstractAction;
import com.epam.task.web.util.logging.LoggerUtil;
import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class EditOrder extends AbstractAction {

    private final LoggerUtil logger = new LoggerUtil(Logger.getLogger(EditOrder.class.getClass()));

    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        DataBaseWriter dataBaseWriter = new DataBaseWriter();
        Order order = null;
        ResultSet resultSet = null;

        if (request.getParameter("update").equals("false")) {
            resultSet = find("oc_order", "order_id", request.getParameter("order_id"));
            try {
                if (resultSet.first()) {
                    long id = (long) resultSet.getObject("order_id");
                    order = createOrderById(id);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            session.setAttribute("oc_order", order);
            return "/editOrder.jsp";
        } else {
            try {
                long id = Integer.valueOf(request.getParameter("order_id"));
                Date dateAdded = (new SimpleDateFormat("yyyy-MM-dd")).parse(request.getParameter("date_added"));
                Status status = createStatusById((long) findField(find("oc_order_status", "name", request.getParameter("name")), "order_status_id"));
                User customer = createUserById(Long.valueOf(request.getParameter("user_id")));
                User manager = createUserById(Long.valueOf(request.getParameter("manager_id")));
                String comment = request.getParameter("comment");
                OrderProduct orderProduct = createOrderProductById(Long.valueOf(request.getParameter("order_id")));

                Map<String, Object> conditions = new HashMap<>();
                conditions.put("date_added", new SimpleDateFormat("yyyy-MM-dd").format(dateAdded));
                conditions.put("status_id", status.getStatusId());
                conditions.put("user_id", customer.getId());
                conditions.put("manager_id", manager.getId());
                conditions.put("comment", comment);
                conditions.put("order_id", orderProduct.getId());

                Map<String, Object> whereCon = new HashMap<>();
                whereCon.put("order_id", id);
                dataBaseWriter.update("oc_order", conditions, whereCon);
            } catch (ParseException exception) {
                logger.getExceptionTextFileLogger().error(exception);
            }

            return "/managerPage.jsp";
        }
    }
}
